package com.rs.service.impl.rs;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.DocumentMapper;
import com.rs.dao.JournalMapper;
import com.rs.model.CommonResult;
import com.rs.model.Journal;
import com.rs.model.Relation;
import com.rs.model.rs.User;
import com.rs.service.rs.JournalService;
import com.rs.util.TimeTypeConversionUtil;

@Service
public class JournalServiceImpl implements JournalService {
	@Autowired
	private JournalMapper jm;

	@Autowired
	private DocumentMapper dm;

	/******App******/

	/**
	 * 添加项目异动日志申请
	 */
	@Override
	@Transactional
	public CommonResult<String> journalAddApp(Journal journal) {
		//判断申请人是否为该项目的人
		//根据申请人id和项目id
		Relation relation = dm.judgeUpUserRelation(journal.getJ_uid(), journal.getJ_prid());
		boolean flag = Optional.ofNullable(relation).isPresent();
		if (!flag) {//申请人不是该项目组的人
			return new CommonResult<String>(200, "只有该项目组的人才可以申请");
		}
		journal.setJ_time(new Date());//设置时间
		journal.setJ_audit(0);//将审核状态设为0，待审核
		int count = jm.journalAddApp(journal);
		String msg = null;
		if (count != 0) {
			msg = "申请成功";
		} else {
			msg = "申请失败";
		}
		return new CommonResult<String>(200, msg);
	}

	/**
	 * 查询本人所申请的日志记录
	 */
	@Override
	public CommonResult<List<Journal>> journalOwnApplicationQueryApp(Journal journal) {
		List<Journal> list = jm.journalOwnApplicationQueryApp(journal);
		return new CommonResult<List<Journal>>(200, "查询成功", list);
	}

	/**
	 * 查询日志详情
	 */
	@Override
	public CommonResult<Journal> journalOneQueryApp(Integer j_id) {
		Journal journal = jm.journalOneQueryApp(j_id);
		return new CommonResult<Journal>(200, "查询成功", journal);
	}

	/**
	 * 修改日志
	 */
	@Override
	@Transactional
	public CommonResult<String> journalUpdateApp(Journal journal) {

		//判断修改人是否为创建人
		Journal j = jm.journalOneQueryApp(journal.getJ_id());
		if (!j.getJ_uid().equals(journal.getJ_uid())) {
			return new CommonResult<String>(200, "修改人只能为日志创建者");
		}
		journal.setJ_uptime(new Date());
		String msg = null;
		int count = jm.journalUpdateApp(journal);
		if (count != 0) {
			msg = "修改成功";
		} else {
			msg = "修改失败";
		}
		return new CommonResult<String>(200, msg);
	}

	/**
	 * 删除待审核和未通过的日志
	 */
	@Override
	public CommonResult<String> journalDelectApp(Journal journal) {
		//判断删除人是否为创建人
		Journal j = jm.journalOneQueryApp(journal.getJ_id());
		if (!j.getJ_uid().equals(journal.getJ_uid())) {
			return new CommonResult<String>(200, "删除人只能为日志创建者");
		}
		if (j.getJ_audit() == 1) {//审核状态为通过
			return new CommonResult<String>(200, "只能删除待审核和未通过的日志");
		}
		String msg = null;
		int count = jm.deleteByPrimaryKey(journal.getJ_id());
		if (count != 0) {
			msg = "删除成功";
		} else {
			msg = "删除失败";
		}
		return new CommonResult<String>(200, msg);
	}

	/**
	 * 审核时查询所有待审核的日志
	 */
	@Override
	public CommonResult<List<Journal>> journalAuditLookAllApp() {
		List<Journal> list = jm.journalAuditLookAllApp();
		return new CommonResult<List<Journal>>(200, "查询成功", list);
	}

	/**
	 * 审核日志
	 */
	@Override
	public CommonResult<String> journalAuditApp(Journal journal) {
		int count = jm.journalAuditApp(journal);
		String msg = null;
		if (count != 0) {
			msg = "审核成功";
		} else {
			msg = "审核失败";
		}
		return new CommonResult<String>(200, msg);
	}

	/*************pc*************/

	/**
	 * pc端查询所有（不传时，查所有，传值时为条件查询）
	 */
	@Override
	public CommonResult<Map<String, Object>> journalQueryAllPc(Journal journal) {
		Page<Journal> page = PageHelper.startPage(journal.getPageNum(), 10);
		List<Journal> list = jm.journalQueryAllPc(journal);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("total", page.getTotal());

		return new CommonResult<Map<String, Object>>(200, "查询成功", map);
	}

	/**
	 * pc端按时间查询
	 */
	@Override
	public CommonResult<Map<String, Object>> journalQueryByTimePc(Map<String, String> map) {
		Integer pageNum = Integer.valueOf(map.get("pageNum"));//当前页
		Date startTime = TimeTypeConversionUtil.getDateByString(map.get("startTime"));//开始时间
		Date endTime = TimeTypeConversionUtil.getDateByString(map.get("endTime"));//结束时间
		Integer type = Integer.valueOf(map.get("type"));//查询类型（0：建立时间 1：修改时间）

		Page<User> page = PageHelper.startPage(pageNum, 10);//分页
		List<User> list = jm.journalQueryByTimePc(startTime, endTime, type);//list

		//map
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("list", list);
		mm.put("total", page.getTotal());
		return new CommonResult<Map<String, Object>>(200, "查询成功", mm);
	}

	/**
	 * pc查询详情
	 */
	@Override
	public CommonResult<Journal> journalCheckDetailsPc(Integer j_id) {
		Journal journal = jm.journalOneQueryApp(j_id);
		return new CommonResult<Journal>(200, "查询成功", journal);
	}

}
